<template>
  <div class="reset">
    <p class="theme">修改密码</p>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="新密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="10">
          <el-form-item label="确认密码" prop="password2">
            <el-input v-model="ruleForm.password2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import api from "@/api/auth";

const SUCCESS = 0;
export default {
  data() {
    return {
      ruleForm: {
        password: "",
        password2: ""
      },
      rules: {
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "密码长度在 6 到 16 个字符",
            trigger: "blur"
          }
        ],
        password2: [
          { required: true, message: "请确认密码", trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "密码长度在 6 到 16 个字符",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
  },
  methods: {
    handleSubmit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (this.ruleForm.password === this.ruleForm.password2) {
            this._updatePassword();
          } else {
            this.$message.error("两次密码不一致,请重新确认");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    _updatePassword() {
      let user = {
        username: this.$store.state.username,
        password: this.ruleForm.password
      }
      api.updatePassword(user).then(response => {
        if (response.code == 0) {
          this.$message({
            type: "success",
            message: "操作成功"
          });
          this.$router.replace({ path: "/" });
        } else {
          this.$message({
            type: "info",
            message: "操作失败"
          });
        }
      });
    }
  }
};
</script>

<style lang="less">
.reset {
  .theme {
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    margin-bottom: 30px;
  }
}
</style>
